package org.simple.framework.core.util;

import java.io.*;

/**
 * 项目名称：java-leaning
 * 类名称：DeepCloneable
 * 类描述：DeepCloneable 深克隆接口
 * 创建时间：2018/7/16
 *
 * @author sisyphus   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
public interface DeepCloneable<T> extends Serializable {

    @SuppressWarnings("unchecked")
    default T deepClone() throws Exception {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(this);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        return (T) objectInputStream.readObject();
    }

    ;
}
